package com.care.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.care.entity.Consult;
import com.care.entity.Doctor;
import com.care.entity.Page;
import com.care.entity.User;
import com.care.service.AdminServiceImpl;
import com.care.service.UserServiceImpl;



@Controller
public class UserContorller {
	
	@Resource
	private UserServiceImpl usi;
	@Resource
	private AdminServiceImpl asi;
	
	

	
	
	//注册
	@RequestMapping(value="register",method=RequestMethod.POST)
	public String Register(HttpServletRequest request){
		System.out.println(12);
		String name = request.getParameter("name");
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String sex = request.getParameter("sex");
		usi.Register(name, email, password, sex);
		return "/login.jsp";
	}
	
	
	
	//添加问题
	@RequestMapping("addconsult")
	public String addConsult(HttpServletRequest request,HttpSession session){
		User user = (User)session.getAttribute("user");
		String content = request.getParameter("content");
		String type = request.getParameter("type");
		usi.addConsult(content, type, user);
		return "查询自己提问过的问题";
	}
	
	
	
	//删除问题
	@RequestMapping("/delconsult/{id}")
	public String delConsult(HttpServletRequest request,@PathVariable("id") Integer id){
		usi.delConsult(id);
		return "查询自己提问过的问题";
	}
	
	
	
	//查询自己提问过的问题
	@RequestMapping("/selectmyconsult/{number}")
	public String selectMyConsult(HttpServletRequest request,HttpSession session,@PathVariable("number") String number){
		User user = (User)session.getAttribute("user");
		Page<Consult> selAllConsult = usi.selAllConsult(user, number);
		request.setAttribute("selAllConsult", selAllConsult);
		return "问题的分页";
	}
	//查看一个问题
	@RequestMapping("/selectoneconsult/{id}")
	public String selectOneConsult(HttpServletRequest request,@PathVariable("id") Integer id){
		Consult selConsult = usi.selConsult(id);
		request.setAttribute("selConsult", selConsult);
		return "一个问题的页面";
	}
	//查询不同科室的问题(上一页下一页记得标上type属性)
	@RequestMapping("/seltypeconsults/{type}/{number}")
	public String selTypeConsult(HttpServletRequest request,@PathVariable("type") String type,@PathVariable("number") String number){
		Page<Consult> selTypeConsult = usi.selTypeConsult(type, number);
		request.setAttribute("selTypeConsult", selTypeConsult);
		return "问题的分页";
	}
	//查看向某医生咨询的所有问题
	@RequestMapping("/seltypeconsults/{doctorname}")
	public String selDocConsult(HttpServletRequest request,@PathVariable("doctorname") String doctorname){
		List<Consult> selDocConsult = usi.selDocConsult(doctorname);
		request.setAttribute("selDocConsult", selDocConsult);
		return "随便一个页面";
	}
	
	
	
	//更改自己问题
	@RequestMapping("/updconsult")
	public String updConsult(HttpServletRequest request,HttpSession session){
		User user =(User) session.getAttribute("user");
		int id = Integer.parseInt(request.getParameter("id"));
		String content = request.getParameter("content");
		String type = request.getParameter("type");
		usi.updConsult(id, content, type, user);
		return "首页";
	}
	//改自己
	@RequestMapping("/updusers")
	public String updUser(HttpServletRequest request){
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String sex = request.getParameter("sex");
		String eamil = request.getParameter("eamil");
		if(usi.updUser(name, password, sex, eamil)){
			return "首页";
		}return "首页";
	}
	/**
	 * 添加咨询且扣除用户钱 到咨询表
	 * @param request
	 * @param response
	 * @param consult
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("addConsultByUser")
	public String addConsultByUser(HttpServletRequest request,HttpServletResponse response,Consult consult) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");	
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");
		String name=request.getParameter("name");
		Doctor doctor=this.usi.findDoctorByName(name);
		if (this.usi.addConsultByUser(consult, doctor, user)){
		response.getWriter().write("<script> alert('咨询成功,等待医生回复');window.location='consultByuc'</script>");		
		}else {
			response.getWriter().write("<script> alert('当前用户余额不足,请更换提问金额');window.location='DoctorPage'</script>");		
		}		
		return null;		
	}
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @param consult
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("deleteConsultByUser")
	public String deleteConsultByUser(HttpServletRequest request,HttpServletResponse response,Consult consult) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");		
		this.usi.deleteConsultByUser(consult, user);
		response.getWriter().write("<script> alert('取消成功');window.location='consultByuc'</script>");		
		return null;
		
	}
}
